<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/6/13
 * Time: 11:50
 */

namespace app\common\cachemodel;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

/**
 * 校准生辰结果模型
 * Class RectificationResult
 * @package app\common\cachemodel
 */
class RectificationResult extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_rectification_result';

    protected $cacheModel = 'rectification.rectification_result';

    /**
     * 添加校准生辰结果
     * @param $data
     * @return bool
     */
    public function SaveData($data)
    {
        try
        {
            $this->saveAll($data);
        }
        catch (\Exception $e)
        {
            return false;
        }
        return true;
    }

    /**
     * 更新结果
     * @param $uid
     * @param $reid
     * @param $id
     * @return bool
     * @throws \Exception
     */
    public function UpdateData($uid, $reid, $id)
    {
        try
        {
            if ($id)
            {
                $this
                    ->alias('rer')
                    ->join('wechat_backup_07_18_14_43_00.app_rectification re', 're.id=rer.reid')
                    ->where(['re.uid' => $uid, 'rer.status' => 1])
                    ->update(['rer.status' => 0]);
                $this->where(['id' => $id])->update(['status' => 1]);
            }
        }
        catch (\Exception $e)
        {
            $this->error = '更新失败';
            return false;
        }
        CacheManage::getInstance()->setCacheConfig($this->cacheModel, $reid)->rmCache();
        return true;
    }


    /**
     * 数据库批量获取校准生辰结果数据
     * @param int $cid
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $field = 'id,status,reid,time,score';
        try
        {
            $data = $this->where('reid', 'in', $ids)
                ->field($field)
                ->selectOrFail();
            $data = json_decode(json_encode($data, true), true);
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }
}